package com.jbh.wanandroidkotlin.refreshview

import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.util.dipToPx
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshKernel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import kotlinx.android.synthetic.main.activity_login.view.*

class PullRefreshHeader : LinearLayout , RefreshHeader{

    private var loadingView : ImageView?=  null
    private var animationDrawable : AnimationDrawable ?= null

    constructor(context: Context):this(context, null)

    constructor(context: Context, attrs: AttributeSet?):this(context, attrs, 0)

    constructor(ctx: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        ctx,
        attrs,
        defStyleAttr
    ) {

        var lay_content = LinearLayout(ctx)
        lay_content.gravity = Gravity.CENTER
        addView(lay_content, LayoutParams(LayoutParams.MATCH_PARENT, dipToPx(ctx, 100)))


        loadingView = ImageView(ctx)
        lay_content.addView(loadingView,LayoutParams( dipToPx(ctx, 30), dipToPx(ctx, 30)))

        loadingView?.setImageResource(R.drawable.progress_loading)
        animationDrawable = resources.getDrawable(R.drawable.progress_round) as AnimationDrawable
        loadingView?.setImageDrawable(animationDrawable)

    }



    /**
     * 2，获取真实视图（必须返回，不能为null）一般就是返回当前自定义的view
     */
    override fun getView(): View {
        return this
    }


    /**
     * 3，获取变换方式（必须指定一个：平移、拉伸、固定、全屏）,Translate指平移，大多数都是平移
     */
    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate
    }



    /**
     * 4，执行下拉的过程
     *
     * @param isDragging
     * @param percent
     * @param offset
     * @param height
     * @param maxDragHeight
     */
    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {
        println("onMoving = $percent")
        if (percent < 1) {
            loadingView?.scaleX = percent
            loadingView?.scaleY = percent
        }
    }



    /**
     * 5，一般可以理解为一下case中的三种状态，在达到相应状态时候开始改变
     * 注意：这三种状态都是初始化的状态
     */
    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        when(newState){

            RefreshState.PullDownToRefresh -> {
                println("Refresh->下拉刷新")
                animationDrawable?.stop()
            }

            RefreshState.ReleaseToRefresh -> {
                println("Refresh->释放刷新")
                animationDrawable?.stop()
            }


            RefreshState.Refreshing -> {
                println("Refresh->正在刷新中")
                animationDrawable!!.start()
            }

        }
    }


    /**
     * 6，结束下拉刷新的时候需要关闭动画
     *
     * @param refreshLayout
     * @param success
     * @return
     */
    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        return 0
    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
    }
    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {
    }

    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }

    override fun setPrimaryColors(vararg colors: Int) {
    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
    }

}